package basic.sort.violence;

import java.util.Stack;

/**
 * @author distantstar
 * @version 1.0
 * @date 2021
 * @description basic.sort.violence
 * @since JDK1.8
 */
public class ReverseStack {

    /**
     * 移除栈底元素并返回
     * @param stack 栈
     * @return 返回栈底元素
     */
    private static int popBottom(Stack<Integer> stack) {
        int cur = stack.pop();
        if (stack.isEmpty()) {
            return cur;
        }

        int last = popBottom(stack);
        stack.push(cur);
        return last;
    }

    private static void reverse(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        int i = popBottom(stack);
        reverse(stack);
        stack.push(i);
    }

    public static void main(String[] args) {
        Stack<Integer> test = new Stack<>();
        test.push(1);
        test.push(2);
        test.push(3);
        test.push(4);
        test.push(5);
        reverse(test);
        while (!test.isEmpty()) {
            System.out.println(test.pop());
        }
    }
}
